<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
        /* let user;
        // fetch all users
        $.get('https://api.github.com/users',data=>{
            console.log('fetch all users')
            user=data[0].login;

            // fetch the first users's repos
            $.get(`https://api.github.com/users/${user}/repos`, data => {
                console.log('fetch users repos')
                console.log(data)
            })
        }) */

        let username;
        let usersPromise = axios.get('https://api.github.com/users')
        usersPromise
            .then(respone => {
                username = respone.data[0].login
                return axios.get(`https://api.github.com/users/${username}/repos`)
            })
            .then(respone => {
                // console.log(respone.data);
            })
            .catch(err => {
                console.log(err)
            })

        const p = new Promise((resolve, reject) => {
            // resolve('awesome!')
            reject(Error('not awesome!'))
        })

        p.then(data => {
            console.log(data)
        }).catch(err => {
            console.log(err)
        })

        const repos = [
            { id: 1, name: 'grit', owner: 'mojobo', desc: 'grit is no longer maintained' },
            { id: 2, name: 'jsawesome', owner: 'vanpelt', desc: 'awesome json' },
            { id: 3, name: 'merb-core', owner: 'wycats', desc: 'merb core:all you need,none you don\'t.' }
        ]

        const owners = [
            { name: 'mojobo', location: 'san francisco', follower: 123 },
            { name: 'vanpelt', location: 'bay minette', follower: 123 },
            { name: 'wycats', location: 'los angeles,ca', follower: 123 }
        ]

        function getRepoById(id) {
            return new Promise((resolve, reject) => {
                const repo = repos.find(repo => repo.id === id)
                if (repo) {
                    resolve(repo)
                } else {
                    reject(Error('no repo find'))
                }
            })
        }

        function combineOwener(repo) {
            return new Promise((resolve, reject) => {
                const owner = owners.find(owner => owner.name === repo.owner)
                if (owner) {
                    repo.owner = owner
                    resolve(repo)
                } else {
                    reject(Error('no find the owner'))
                }
            })
        }

        getRepoById(1).then(repo => {
            return combineOwener(repo)
        }).then(repo => {
            console.log(repo)
        }).catch(err => {
            console.log(err)
        })

        const users = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve(['mojobo', 'vanpelt', 'wycats'])
            }, 2000)
        })

        const movies = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve({ name: 'ECMAScript', alias: 'es6', year: 2013 })
            }, 500)
        })

        Promise.all([users, movies]).then(resp => {
            console.log(resp)
        })



        let methodName = 'info';
        class User {
            constructor(name, email) {
                this.name = name;
                this.email = email;
            }
            [methodName]() {
                console.log(`hi,I'm ${this.name},my email is ${this.email}`);
            }
            static desc() {
                console.log(`I am a user`);
            }
            set github(name) {
                this.name = name;
            }
            get github() {
                return `https://github.com/${this.name}`;
            }
        }

        const user = new User('jean', 'jean@gmail.com')

        console.log(user);
        console.log(user.info());

        // function User(name, email) {
        //     this.name = name;
        //     this.email = email;
        // }

        // User.prototype.info = function () {
        //     console.log(`hi,I'm ${this.name},my email is ${this.email}`);
        // }


    </script>
</body>

</html>